//JavaScript Framework 2.0 Code
try {
	Type.registerNamespace('com.yonyou.ncmm_pac_pda.InvCountScanPageController');
	com.yonyou.ncmm_pac_pda.InvCountScanPageController = function() {
		com.yonyou.ncmm_pac_pda.InvCountScanPageController.initializeBase(this);
		this.initialize();
	}

	function com$yonyou$ncmm_pac_pda$InvCountScanPageController$initialize() {
		//you can programing by $ctx API
		//get the context data through $ctx.get()
		//set the context data through $ctx.push(json)
		//set the field of the context through $ctx.put(fieldName, fieldValue)
		//get the parameter of the context through $ctx.param(parameterName)
		//Demo Code:
		//    var str = $ctx.getString();      //获取当前Context对应的字符串
		//    alert($ctx.getString())          //alert当前Context对应的字符串
		//    var json = $ctx.getJSONObject(); //获取当前Context，返回值为json
		//    json["x"] = "a";        //为当前json增加字段
		//    json["y"] = [];           //为当前json增加数组
		//    $ctx.push(json);            //设置context，并自动调用数据绑定
		//
		//    put方法需手动调用databind()
		//    var x = $ctx.get("x");    //获取x字段值
		//    $ctx.put("x", "b");     //设置x字段值
		//    $ctx.put("x", "b");     //设置x字段值
		//    $ctx.databind();            //调用数据绑定才能将修改的字段绑定到控件上
		//    var p1 = $param.getString("p1");   //获取参数p2的值，返回一个字符串
		//    var p2 = $param.getJSONObject("p2");   //获取参数p3的值，返回一个JSON对象
		//    var p3 = $param.getJSONArray("p3");   //获取参数p1的值，返回一个数组

		//your initialize code below...

	}

	function com$yonyou$ncmm_pac_pda$InvCountScanPageController$evaljs(js) {
		eval(js)
	}

	function com$yonyou$ncmm_pac_pda$InvCountScanPageController$button2_onclick(sender, args) {
		// 对于修改的数据进行暂存，保存到本地
		$ctx.dataCollect();
		var json = $ctx.getJSONObject();
		if(json["vbillcode"] == "" || json["cinvcode"] == "") {
			$toast("当前记录的单据号或物料信息为空，不能提交！");
			return;
		}
		if(json["scansinglenum"] == "") {
			$toast("当前记录的扫描主数量为空，不能提交！");
			return;
		}
		//处理特殊字符
		json = NCMMPubUtil.htmlspecialjson2UI(json);
		//扫描时间
		json["scandate"] = NCMMPubUtil.getNowDateTime();
		// 保存本条数据到本地数据库
		if(json["pkscan"] == "" || json["pkscan"] == null) {
			// 如果没有保存过就保存
			json["pkscan"] = getPKScan();
			var showjson = $.extend({}, json);
			NCMMPubUtil.insertVO(INVCOUNTSCANVO, json);
			$ctx.push(showjson);
		} else {
			var pkscan = json["pkscan"];
			var isvoexist = NCMMPubUtil.isScanVOExistByPK(INVCOUNTSCANVO, pkscan)
			if(!isvoexist) {
				// 在扫描记录界面删除后再跳回采集页面，pkscan不为空，但此时数据已经被删除
				$alert("当前记录在扫描记录中已经被删除，请重新采集！");
				return;
			}
			// 如果保存过就更新
			NCMMPubUtil.updateVO(INVCOUNTSCANVO, json);
		}
		INVCOUNTUtil.updateScanNumForList(json["pkitem"]);
		$toast("当前数据保存成功！");
	}

	function com$yonyou$ncmm_pac_pda$InvCountScanPageController$button3_onclick(sender, args) {
		// 保存到本地，提交到nc端
		$ctx.dataCollect();
		var json = $ctx.getJSONObject();
		if(json["vbillcode"] == "" || json["cinvcode"] == "") {
			$toast("当前记录的单据号或物料信息为空，不能提交！");
			return;
		}
		if(json["scansinglenum"] == "") {
			$toast("当前记录的扫描主数量为空，不能提交！");
			return;
		}
		// 持久化扫描记录到本地数据库
		INVCOUNTUtil.persistRecord(json);
		//更新界面
		$ctx.push(json);
		// 检查扫描表中是否还有其他提交记录
		if(INVCOUNTUtil.checkOtherScanRecord(json["pkitem"]) == true) {
			$toast("当前记录存在其他来源相同的扫描记录，不能单独提交！");
			return;
		}
		//将扫描记录转换为提交NC的数据
		billinfos = INVCOUNTUtil.changeVOTOBillInfos([json]);
		//提交
		INVCOUNTUtil.submitdataToNC([json], billinfos, refreshSubmitdata);
	}
	//刷新提交界面数据
	function refreshSubmitdata() {
		$ctx.push({});
		$toast("当前数据提交成功！");
	}

	function com$yonyou$ncmm_pac_pda$InvCountScanPageController$imagebutton0_onclick(sender, args) {
		if($ctx.getString("vbillcode") == null || $ctx.getString("vbillcode") == "") {
			// 扫物料码必须保证已经有单据码
			$alert("单据号不能为空，请先扫描单据码！");
			$id("textbox0").set("value", null)
			return;
		}
		$scanner.open({
			bindfield: "code",
			callback: "myCallBack()"
		})
	}

	function myCallBack() {
		var code = $ctx.getString("code");
		/*if (code != null && code != "") {
		 // 扫描的是物料码
		 $id("textbox0").set("value", code);
		 $ctx.dataBind();
		 }*/
		parseMaterialBarAndSave(code);
		$id("textbox0").set("value", null)
	}

	function getPKScan() {
		var date = new Date().getTime();
		return "4004" + date.toString() + "1111";
	}

	function com$yonyou$ncmm_pac_pda$InvCountScanPageController$button0_onclick(sender, args) {
		$view.close();
	}

	function com$yonyou$ncmm_pac_pda$InvCountScanPageController$onload(sender, args) {
		var code = $param.getString("code");
		if(code != null && code != "") {
			// 扫描的是单据码
			$id("textbox1").set("value", code);
			$ctx.dataBind();
		}
		// 如果是由成产品详情采集则带入相应信息
		var json = $param.getJSONObject("row");
		if(json != null) {
			//处理反斜杠特殊字符
			json = NCMMPubUtil.htmlspecialjson2UI(json);
			//---Begin--处理采集界面精度--
			var jsonarray = [];
			jsonarray.push(json);
			var numkeys = new Array("nonhandnum", "scansinglenum");
			INVCOUNTUtil.processScale(jsonarray, numkeys, "pk_measdoc");
			var astnumkeys = new Array("nonhandastnum", "scanastnum");
			INVCOUNTUtil.processScale(jsonarray, astnumkeys, "castunitid");
			//---End--------------
			$ctx.push(json);
		}
		if($cache.read("isinvcountscancreated") != "true") {
			NCMMPubUtil.createTableWithAutoPK(INVCOUNTSCANVO);
			$cache.write("isinvcountscancreated", "true");
		}
	}

	function com$yonyou$ncmm_pac_pda$InvCountScanPageController$onchange(sender, args) {
		//$ctx.dataCollect();
		var code = $id("textbox0").get("value");
		var json = {};
		if(code == "") {
			$ctx.push(json);
			return;
		}
		parseMaterialBarAndSave(code);
		$id("textbox0").set("value", null);
		$id("textbox0").focus();
	}

	function parseMaterialBarAndSave(code) {
		var json = {};
		// 解析物料码
		var materialbar = NCMMBarcodeParseUtil.parseMaterialBar(code);
		//$alert(materialbar);
		if(materialbar != null || materialbar != "") {
			var failmsg = materialbar.failmsg;
			if(materialbar["pk_material"] == "" && failmsg != "" && failmsg.length > 0) {
				// 解析如果有错误抛错
				$alert(failmsg);
				return;
			}
			// 取到物料和单据号进行匹配明细行
			var material = materialbar["pk_material"];
			var vbillcode = $ctx.getString("vbillcode");
			var marcode= materialbar["code"];
			var whereSql = "where pkmaterial='" + material.toString() + "'" + " and vbillcode= '" + vbillcode + "'";
			var retdata = NCMMPubUtil.queryVONoCallback(INVCOUNTVO, whereSql);
			if(retdata == null || retdata == "" || retdata.length < 1 || eval(retdata).length < 1) {
				$alert("扫描物料"+marcode+"不在对应的单据"+vbillcode+"中");
				return;
			}
			retdata = eval(retdata);
			if(retdata.length == 1) {
				// 如果匹配到只有一条则继续匹配批次号，扫描物料的批次号和单据上的批次号相同，包括为空的情况，单据上的批次号和扫描的不同则匹配失败
				if(retdata[0]["vbatchcode"] != materialbar["vbatchcode"]) {
					$alert("没有匹配到合适的明细行");
					return;
				} else {
					// 如果匹配到的数据只有一条
					json = retdata[0];
				}
			} else {
				//$alert(materialbar["vbatchcode"]);
				// 如果扫描到的物料匹配到多条，则按照批次号匹配
				var batchrecords = [];
				for(var i = 0; i < retdata.length; i++) {
					if(retdata[i]["vbatchcode"] == materialbar["vbatchcode"])
					// 扫描物料中带有批次号则严格匹配
						batchrecords.push(retdata[i]);
				}
				if(batchrecords == null || batchrecords.length < 1) {
					$alert("没有匹配到合适的明细行");
					return;
				}
				if(batchrecords.length == 1) {
					// 如果批次号只匹配到一行
					json = batchrecords[0];
				}
				if(batchrecords.length > 1) {
					// 如果批次号匹配到多行，则按照数量匹配
					var numrecords = [];
					for(var j = 0; j < batchrecords.length; j++) {
						if(batchrecords[j]["scannum"] == "" || batchrecords[j]["scannum"] < batchrecords[j]["nonhandnum"])
						// 扫描物料中带有批次号则严格匹配
							numrecords.push(batchrecords[j]);
					}
					if(numrecords == null || numrecords.length < 1) {
						// 没有数量符合的记录则随机匹配一条
						json = batchrecords[0];
					} else {
						json = numrecords[0];
					}
				}
			}
			//将库存批次号进行赋值
			json["vbatchcode"] = materialbar["vbatchcode"];
			json["scansinglenum"] = "1";
			var vchangerate = json["vchangerate"];//$ctx.getString("vchangerate");
			var astnum = INVCOUNTUtil.getNastum(json["scansinglenum"], vchangerate);
			json["scanastnum"] = astnum;
			//解析出序列号不为空
			if(NCMMPubUtil.isNotEmpty(materialbar["vserialcode"])) {
				json["serialno"] = materialbar["vserialcode"];
			}
		} else {
			$alert("物料码为空，请重新扫描！");
			return;
		}
		// 解析条码成功带入界面数据
		if(NCMMPubUtil.isEmpty(json))
			return;
		// 如果不存在则创建表，如果存在则不创建
		if($cache.read("isinvcountscancreated") != "true") {
			NCMMPubUtil.createTableWithAutoPK(INVCOUNTSCANVO);
			$cache.write("isinvcountscancreated", "true");
		}
		// 暂存插入本条数据到本地数据库
		json["pkscan"] = getPKScan();
		NCMMPubUtil.insertVO(INVCOUNTSCANVO, json);
		INVCOUNTUtil.updateScanNumForList(json["pkitem"]);
		// 清空条形码
		json["code"] = "";
		//处理精度开始
		var jsonarray = [];
		jsonarray.push(json);
		var numkeys = new Array("nonhandnum", "scansinglenum");
		INVCOUNTUtil.processScale(jsonarray, numkeys, "pk_measdoc");
		var astnumkeys = new Array("nonhandastnum", "scanastnum");
		INVCOUNTUtil.processScale(jsonarray, astnumkeys, "castunitid");
		$ctx.push(json);
		$toast("当前数据暂存成功！");
	}

	function com$yonyou$ncmm_pac_pda$InvCountScanPageController$imagebutton1_onclick(sender, args) {
		$menu.openDropDownList({
			"controlid": "imagebutton1",
			"dropDownListWidth": "100", //菜单项的宽度
			"background": "#f2f2f2",
			//"background-image":"app.png",
			"color": "#ee4311",
			"font-size": "12",
			"halign": "center", //right  center
			"split-color": "#ee4311",
			//"margin-left":"10",
			"panelstyle": "round-div",
			"border-color": "#ee4311",
			"dropItemsArray": [{
				"name": "主界面",
				"action": "menu1_click()"
			}, {
				"name": "数据管理",
				"action": "menu2_click()"
			}, {
				"name": "业务设置",
				"action": "menu3_click()"
			}, {
				"name": "注销",
				"action": "menu4_click()"
			}, {
				"name": "扫描记录",
				"action": "menu5_click()"
			}]
		})
	}

	function menu1_click() {
		// 跳转到主界面
		$view.open({
			viewid: "com.yonyou.ncmm_pac_pda.HomePage", //主界面id
			isKeep: "false" //
		})
	}

	function menu2_click() {
		// 跳转到数据管理界面
		$view.open({
			viewid: "com.yonyou.ncmm_pac_pda.DM_main",
			isKeep: "false"
		})
	}

	function menu3_click() {
		// 跳转到业务设置界面
		$view.open({
			viewid: "com.yonyou.ncmm_pac_pda.BusinessSettingPage",
			isKeep: "false"
		})
	}

	function menu4_click() {
		// 跳转到登录界面
		$view.open({
			viewid: "com.yonyou.ncmm_pac_pda.LoginPage", //主界面id
			isKeep: "false"
		})
	}

	function menu5_click() {
		// 扫描记录打开扫描记录列表画面
		$view.open({
			viewid: "com.yonyou.ncmm_pac_pda.InvCountScanListPage",
			isKeep: "false"
		})
	}

	function com$yonyou$ncmm_pac_pda$InvCountScanPageController$number3_oninput(sender, args) {
		$ctx.dataCollect();
		var astnum = $ctx.getString("scanastnum");
		if(astnum == null || astnum == "" || astnum == "-") {
			$ctx.put("scansinglenum", astnum);
			$ctx.dataBind();
			return;
		}
		if(!INVCOUNTUtil.isNumber(astnum)) {
			//此时第一次输入
			var num = $ctx.getString("scansinglenum");
			if(num == null || num == "") {
				$ctx.put("scanastnum", "");
				$ctx.put("scansinglenum", "");
				$ctx.dataBind();
			} else {
				$ctx.put("scanastnum", args["oldvalue"]);
				$ctx.dataBind();
			}
			return;
		}
		if(!INVCOUNTUtil.validateNumScale(astnum, $ctx.getString("castunitid"))) {
			$ctx.put("scanastnum", args["oldvalue"]);
			$ctx.dataBind();
			return;
		}
		var vchangerate = $ctx.getString("vchangerate");
		var num = INVCOUNTUtil.getNum(astnum, vchangerate);
		num = INVCOUNTUtil.getScaleNum(num, $ctx.getString("pk_measdoc"));
		$ctx.put("scansinglenum", num);
		$ctx.dataBind();
	}

	function com$yonyou$ncmm_pac_pda$InvCountScanPageController$number2_oninput(sender, args) {
		$ctx.dataCollect();
		var num = $ctx.getString("scansinglenum");
		if(num == null || num == "" || num == "-") {
			$ctx.put("scanastnum", num);
			$ctx.dataBind();
			return;
		}
		if(!INVCOUNTUtil.isNumber(num)) {
			//此时第一次输入
			var astnum = $ctx.getString("scanastnum");
			if(astnum == null || astnum == "") {
				$ctx.put("scansinglenum", "");
			} else {
				$ctx.put("scansinglenum", args["oldvalue"]);
			}
			$ctx.dataBind();
			return;
		}

		if(!INVCOUNTUtil.validateNumScale(num, $ctx.getString("pk_measdoc"))) {
			$ctx.put("scansinglenum", args["oldvalue"]);
			$ctx.dataBind();
			return;
		}
		var vchangerate = $ctx.getString("vchangerate");
		var astnum = INVCOUNTUtil.getNastum(num, vchangerate);
		astnum = INVCOUNTUtil.getScaleNum(astnum, $ctx.getString("castunitid"));
		$ctx.put("scanastnum", astnum);
		$ctx.dataBind();
	}

	//货位按钮先去掉，后面支持再加上<!--<input id="button1" class="textbtnclass" onclick="rackonclick()" type="button"/> -->	
	function rackonclick(sender, args) {
		// 打开货位参照页面
		openRefPage({
			reftype: "PDARACK",
			reftitle: "货位",
			reftablename: "app_bd_rack"
		}, "hname");

	}

	// 打开参照页面
	function openRefPage(refinfo, ctxkey) {
		if(checkEmpty(refinfo.reftype)) {
			$alert("【" + refinfo.reftitle + "】没有可参照的数据");
			return;
		}
		var pk_stordoc = $ctx.getString("pk_stordoc");
		if(pk_stordoc != null && pk_stordoc != "") {
			refinfo["wheresql"] = " where pk_stordoc='" + pk_stordoc + "'";
		}
		$view.open({
			viewid: "com.yonyou.ncmm_pac_pda.RefPage",
			isKeep: "true",
			data: refinfo,
			callback: function() {
				var refdata = $param.getJSONObject("data");
				$ctx.put(ctxkey, refdata["name"]);
				if(ctxkey == "hname") {
					// 用于拼物料条件
					$ctx.put("hcode", refdata["code"]);
					$ctx.put("pk_rack", refdata["pk_rack"]);
				}
				$ctx.dataBind();
			}
		});
	}

	function checkEmpty(reftype) {
		var condition = {
			"ncbillcode": reftype
		};
		var isEmpty = false;
		NCMMPubUtil.queryVOByConditions(AppmappingheadVO, condition, function(data) {
			var jsondata = {
				bdlistdata: eval(data)
			};
			var reftitle = (jsondata.bdlistdata)[0]["appbillname"];
			if((jsondata.bdlistdata)[0]["downloadnum"] == "" || (jsondata.bdlistdata)[0]["downloadnum"] == "0") {
				isEmpty = true;
			}
		});
		return isEmpty;
	}

	com.yonyou.ncmm_pac_pda.InvCountScanPageController.prototype = {
		number2_oninput: com$yonyou$ncmm_pac_pda$InvCountScanPageController$number2_oninput,
		number3_oninput: com$yonyou$ncmm_pac_pda$InvCountScanPageController$number3_oninput,
		imagebutton1_onclick: com$yonyou$ncmm_pac_pda$InvCountScanPageController$imagebutton1_onclick,
		onchange: com$yonyou$ncmm_pac_pda$InvCountScanPageController$onchange,
		onload: com$yonyou$ncmm_pac_pda$InvCountScanPageController$onload,
		button0_onclick: com$yonyou$ncmm_pac_pda$InvCountScanPageController$button0_onclick,
		imagebutton0_onclick: com$yonyou$ncmm_pac_pda$InvCountScanPageController$imagebutton0_onclick,
		button3_onclick: com$yonyou$ncmm_pac_pda$InvCountScanPageController$button3_onclick,
		button2_onclick: com$yonyou$ncmm_pac_pda$InvCountScanPageController$button2_onclick,
		initialize: com$yonyou$ncmm_pac_pda$InvCountScanPageController$initialize,
		evaljs: com$yonyou$ncmm_pac_pda$InvCountScanPageController$evaljs
	};
	com.yonyou.ncmm_pac_pda.InvCountScanPageController.registerClass('com.yonyou.ncmm_pac_pda.InvCountScanPageController', UMP.UI.Mvc.Controller);
} catch(e) {
	$e(e);
}